package com.biz.primus.auth.controller;

import com.biz.primus.auth.feign.MenuGroupFeignClient;
import com.biz.primus.auth.feign.RoleFeignClient;
import com.biz.primus.base.enums.CommonStatus;
import javax.validation.Valid;

import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.model.sso.auth.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * RoleController
 *
 * @author guisheng.deng
 * @date 2017年04月20日
 * @reviewer
 * @description
 * @see
 */
@RequestMapping("/roles")
@Secured("ROLE_ROLE")
@Controller
public class RoleController extends AbstractBaseController {

    private final RoleFeignClient roleFeignClient;

    private final MenuGroupFeignClient menuGroupFeignClient;

    @Autowired
    public RoleController(RoleFeignClient roleFeignClient, MenuGroupFeignClient menuGroupFeignClient) {
        this.roleFeignClient = roleFeignClient;
        this.menuGroupFeignClient = menuGroupFeignClient;
    }


    @GetMapping
    @PreAuthorize("hasAuthority('OPT_ROLE_LIST')")
    public String listRoles(Model model, @RequestParam(value = "status", required = false, defaultValue = "ENABLE") CommonStatus status) {
        model.addAttribute("roles", roleFeignClient.findByStatus(status));
        return "role/roleList";
    }

    @RequestMapping("/addOrUpdate")
    @PreAuthorize("hasAuthority('OPT_ROLE_EDIT')")
    public String addOrUpdate(@Valid RoleVo role, BindingResult result) {
        error(result);
        roleFeignClient.addOrUpdate(role);
        return "redirect:/roles";
    }


    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('OPT_ROLE_EDIT')")
    public String add(Model model) {
        model.addAttribute("cmd", "add");
        model.addAttribute("mainmenus", menuGroupFeignClient.findEnabledMenuGroups());
        return "role/addOrUpdateRole";
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('OPT_ROLE_EDIT')")
    public String edit(Model model, @RequestParam("id") Long id) {
        RoleVo role = id != null ? roleFeignClient.getRole(id) : null;
        model.addAttribute("role", role);
        model.addAttribute("cmd", "edit");
        model.addAttribute("mainmenus", menuGroupFeignClient.findEnabledMenuGroups());
        return "role/addOrUpdateRole";
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('OPT_ROLE_DELETE')")
    @ResponseBody
    public Boolean delete(@RequestParam("id") Long id) {
        //逻辑删除,不是物理删除。
        if (id != null) {
            roleFeignClient.delete(id);
            return true;
        }
        return false;
    }

    @RequestMapping("/isExist")
    @ResponseBody
    public Boolean isExist(RoleVo role) {
        return roleFeignClient.isExist(role);
    }

}